package com.lanou.controller;

import com.lanou.bean.Cost;
import com.lanou.service.CostService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by lanouhn on 17/10/21.
 */
@Controller
public class CostController {

    @Resource
    private CostService costService;

    //  查询所有的数据
    @RequestMapping("/selectAllCostInfo")
    public String selectAllCostInfo(HttpSession session) {
        List<Cost> costList = costService.selectAllCostInfo();
        session.setAttribute("costlist", costList);
        return "fee/fee_list";
    }

    //    插入数据
    @RequestMapping("/insertCost")
    public String insertCost(String name, String base_duration, String base_cost, String unit_cost, String descr, String radFeeType) {
        System.out.println("执行插入数据");
        System.out.println(name + base_cost + base_duration + unit_cost + descr + radFeeType);
        int result = costService.insertCost(name, base_duration, base_cost, unit_cost, descr);
        System.out.println(result);
        return "fee/fee_list";
    }

    //  删除数据
    @RequestMapping("/deleteCostById")
    public String deleteCostById(Integer cost_id) {
//        删除中间表关系service表
        int resule1 = costService.deleteserviceById(cost_id);
        System.out.println(resule1 + "删除中间表关联");
        System.out.println("资费管理数据的删除操作:" + cost_id);
        int result = costService.deleteCostById(cost_id);
        System.out.println(result);
        return "fee/fee_list";
    }

    //    查询所有的信息进行升序排序
    @RequestMapping("/selectAllCostAndSort")
    public String selectAllCostAndSort(HttpSession session) {
        System.out.println("执行排序操作");
        List<Cost> selectAllCostAndSort = costService.selectAllCostAndSort();
        session.setAttribute("costlist", selectAllCostAndSort);
        return "fee/fee_list";
    }

//    根据时长进行排序
    @RequestMapping("/selectAllCostAndSortByCost")
    public String selectAllCostAndSortByCost(HttpSession session){

        List<Cost> selectAllCostAndSortByCost=costService.selectAllCostAndSortByCost();
        session.setAttribute("costlist",selectAllCostAndSortByCost);
        return "fee/fee_list";
    }

//    根据id进行数据的回显
    @RequestMapping("/selectByPrimaryKey")
    public String selectByPrimaryKey(Integer cost_id,HttpSession session){
        System.out.println(cost_id);

        Cost cost=costService.selectByPrimaryKey(cost_id);
        System.out.println(cost);
        session.setAttribute("cost",cost);
        return "fee/fee_modi";

    }

//    根据id进行更新操做
    @RequestMapping("/updateCost")
    public String updateCost(Integer cost_id,String name,String base_duration,String base_cost,String unit_cost,String descr){
        System.out.println(cost_id+name+base_duration+base_cost+unit_cost+descr);

        int result=costService.updateCost(cost_id,name,base_duration,base_cost,unit_cost,descr);
        System.out.println(result);
        return "fee/fee_list";
    }

//    根据id更新状态，记录时间
    @RequestMapping("/updateCostStatus")
    public String updateCostStatus(Integer cost_id,Integer status,String creatime){
        System.out.println("执行更新状态的操作");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        creatime=df.format(new Date());
        status=1;
        int result=  costService.updateCostStatus(cost_id,status,creatime);
        System.out.println(result);
        return "fee/fee_list";
    }

    //资费名称详情
    @RequestMapping("/selectCostByName")
    public String selectCostByName(Integer cost_id, HttpServletRequest request){
        System.out.println(cost_id);
        Cost cost=costService.selectCostByName(cost_id);
        System.out.println(cost);
        request.setAttribute("cost",cost);
        return "fee/fee_detail";
    }


}